#include <iostream>

#include <string>
#include <vector>

#include <algorithm>

// 方法1：将整数转为字符串后遍历
void traverse_digits_ascending_str(int num) {
    std::string s = std::to_string(num);
    std::cout << "Digits (ascending): ";
    for (char c : s) {
        std::cout << c << " ";
    }
    std::cout << std::endl;
}

// 方法2：使用数学方法提取最高位到最低位
void traverse_digits_ascending_math(int num) {
    std::cout << "Digits (ascending): ";
    
    if (num == 0) {
        std::cout << "0" << std::endl;
        return;
    }
    
    // 处理负数
    if (num < 0) {
        std::cout << "- ";
        num = -num;
    }
    
    // 计算数字的位数
    int temp = num;
    int digit_count = 0;
    while (temp > 0) {
        digit_count++;
        temp /= 10;
    }
    
    // 从最高位开始提取
    int divisor = 1;
    for (int i = 1; i < digit_count; i++) {
        divisor *= 10;
    }
    
    for (int i = 0; i < digit_count; i++) {
        int digit = num / divisor;
        std::cout << digit << " ";
        num %= divisor;
        divisor /= 10;
    }
    std::cout << std::endl;
}

// 方法3：将数字转为向量后遍历
std::vector<int> get_digits_ascending(int num) {
    std::vector<int> digits;
    
    if (num == 0) {
        digits.push_back(0);
        return digits;
    }
    
    // 处理负数
    bool is_negative = num < 0;
    if (is_negative) num = -num;
    
    // 计算位数并提取
    int temp = num;
    int digit_count = 0;
    while (temp > 0) {
        digit_count++;
        temp /= 10;
    }
    
    int divisor = 1;
    for (int i = 1; i < digit_count; i++) {
        divisor *= 10;
    }
    
    for (int i = 0; i < digit_count; i++) {
        digits.push_back(num / divisor);
        num %= divisor;
        divisor /= 10;
    }
    
    return digits;
}

// 使用示例
int main() {
    int num = 12345;
    
    traverse_digits_ascending_str(num);      // 输出: Digits (ascending): 1 2 3 4 5
    traverse_digits_ascending_math(num);     // 输出: Digits (ascending): 1 2 3 4 5
    
    std::vector<int> digits = get_digits_ascending(num);
    std::cout << "Digits vector: ";
    for (int d : digits) {
        std::cout << d << " ";
    }
    std::cout << std::endl;
    
    return 0;
}
